'use strict';

const Service = require('egg').Service;
class UserService extends Service {

  async login(data){
      const result = {}
      try {
        const uid = await this.app.mysql.get('table_admin', { user_name: data.username });
        if(uid){
            if(uid.password === data.password){
              result.code = 200,
              result.message = '登录成功!'
              const token = this.app.jwt.sign({
                  username:'admin',
              },this.app.config.jwt.secret)
              result.token = token
            }else {
              result.code = 201,
              result.message = '账号或密码不正确!'
            }
        }else {
          result.code = 201,
          result.message = '账号不存在!'
        }
      } catch (error) {
        result.code = 202,
        result.message = error.message
      }
      
      return result
  }
}

module.exports = UserService;